<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自动拨号</title>
    <style>
        body {
            margin: 0;
            padding: 20px;
            font-family: Arial, sans-serif;
            background-color: #f0f2f5;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
        }

        .close-button {
            position: absolute;
            top: 10px;
            right: 10px;
            font-size: 24px;
            color: #ff4d4d;
            cursor: pointer;
            background: none;
            border: none;
            padding: 5px;
            line-height: 1;
        }

        .close-button:hover {
            color: #ff0000;
        }

        .container {
            position: relative;
            background-color: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            text-align: center;
            max-width: 400px;
            width: 100%;
        }

        h1 {
            color: #333;
            margin-bottom: 20px;
        }

        .phone-input {
            width: 100%;
            padding: 12px;
            margin: 10px 0;
            border: 2px solid #ddd;
            border-radius: 6px;
            font-size: 16px;
            box-sizing: border-box;
        }

        .call-button {
            background-color: #4CAF50;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
            width: 100%;
        }

        .call-button:hover {
            background-color: #45a049;
        }

        .phone-number {
            font-size: 24px;
            font-weight: bold;
            color: #333;
            margin: 20px 0;
        }

        .status {
            margin-top: 20px;
            color: #666;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <div class="container">
        <button class="close-button" id="closeButton">×</button>
        <h1>马上联系车主</h1>
        <p class="phone-number">点击<span id="cancelIcon" style="cursor: pointer;">❌</span>可取消</p>
        <p class="status" id="status">倒计时开始...</p>
    </div>

    <script>
        function getPhoneFromUrl() {
            const urlParams = new URLSearchParams(window.location.search);
            return urlParams.get('phone');
        }

        const PHONE_NUMBER = getPhoneFromUrl();
        const COUNTDOWN_TIME = 5; // 倒计时秒数
        let countdownInterval;

        window.addEventListener('load', () => {
            if (!PHONE_NUMBER) {
                const statusElement = document.getElementById('status');
                statusElement.textContent = '未找到有效的电话号码';
                return;
            }

            let timeLeft = COUNTDOWN_TIME;
            const statusElement = document.getElementById('status');
            const closeButton = document.getElementById('closeButton');
            const cancelIcon = document.getElementById('cancelIcon');
            
            const cancelCall = () => {
                clearInterval(countdownInterval);
                statusElement.textContent = '已取消自动拨号';
            };
            
            closeButton.addEventListener('click', cancelCall);
            cancelIcon.addEventListener('click', cancelCall);
            
            countdownInterval = setInterval(() => {
                if (timeLeft > 0) {
                    statusElement.textContent = `即将呼叫车主，${timeLeft}秒后自动拨号...`;
                    timeLeft--;
                } else {
                    clearInterval(countdownInterval);
                    makeCall();
                }
            }, 1000);
        });

        function makeCall() {
            const statusElement = document.getElementById('status');
            const telUrl = `tel:${PHONE_NUMBER}`;
            window.location.href = telUrl;
            statusElement.textContent = `正在拨打电话：${PHONE_NUMBER}`;
        }
    </script>
</body>
</html>